import { Injectable, NestInterceptor, ExecutionContext, CallHandler,} from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ResponseUtil } from './response.util';

export interface Response<T> {
 data: T;
}

@Injectable()
export class ResponseInterceptor<T> implements NestInterceptor<T, any> {
 intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
  return next.handle().pipe(
   map((data) => {
    // 如果已经是 ResponseDto 格式，直接返回
    if (data && typeof data === 'object' && 'statusCode' in data) {
     return data;
    }
    // 否则包装成成功响应
    return ResponseUtil.success(data);
   }),
  );
 }
}
